package com.waterworld.watch.common.customview;

import android.media.AudioManager;
import android.media.MediaRecorder;

import java.io.File;
import java.util.UUID;

public class RecorderManager {
    private String dir;
    private String currentFilePath;
    private int volume;
    private MediaRecorder recorder = null;
    private String fileName;

    private static volatile RecorderManager mInstance;

    private boolean isPrepare;
    public RecorderManager(String dir)
    {
        this.dir = dir;
    }
    public static RecorderManager getInstance(String dir){
        if (mInstance == null) {
            synchronized (AudioManager.class) {
                if (mInstance == null) {
                    mInstance = new RecorderManager(dir);
                }
            }
        }
        return mInstance;
    }

    public interface AudioStateListener {
        void wellPrepared();
        void startFailed();
    }

    public AudioStateListener mAudioStateListener;

    public void setOnAudioStateListener(AudioStateListener listener) {
        mAudioStateListener = listener;
    }

    public void prepareAudio() {
        isPrepare = false;
        File dir = new File(this.dir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        fileName = generateFileName();
        File file = new File(dir, fileName);

        currentFilePath = file.getAbsolutePath();
        {
            if (recorder == null) {
                recorder = new MediaRecorder();
                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                recorder.setAudioEncodingBitRate(8);
                recorder.setAudioSamplingRate(8000);
                recorder.setMaxDuration(1000 * 10);//最大录音时长为 10秒
                recorder.setOutputFile(currentFilePath);
                try {
                    recorder.prepare();
                    recorder.start();
                } catch(Exception e){
                    System.out.print(e.getMessage());
                    recorder = null;
                    mAudioStateListener.startFailed();
                }
            }
        }

        if (recorder != null) {
            isPrepare = true;
            if (mAudioStateListener != null) {
                mAudioStateListener.wellPrepared();
            }
        }
    }

    private String generateFileName() {
        return UUID.randomUUID().toString() + ".amr";
    }

    public int getVoiceLevel(int maxlevel) {
        if (isPrepare) {
            double interval = 49/maxlevel;
            double test = (recorder.getMaxAmplitude() / 3000.0);
            if(test > 7)
            {
                volume = 7;
            }
            else if (test < 1)
            {
                test = 1;
            }
            return (int)test;
        }
        return 1;
    }

    public void release() {
        if (recorder != null) {
            try {
                recorder.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
            recorder.release();
            recorder = null;
        }
    }

    public void cancel() {
        release();
        if (currentFilePath != null) {
            File file = new File(currentFilePath);
            file.delete();
            currentFilePath = null;
        }
    }

    public String getCurrentFilePath() {
        return currentFilePath;
    }

    public String getFileName() {
        return fileName;
    }
}
